﻿using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Windows;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using 自定义控件.Views;

namespace 自定义控件
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        protected override void OnStartup(StartupEventArgs e)
        {
            // 只有当Application.Shutdown()方法被调用时，才会将应用程序关闭
            Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            // 方式一
            //{
            //    LoginWindow loginWindow = new LoginWindow();
            //    bool? flag = loginWindow.ShowDialog();

            //    if (flag.HasValue && flag == false)
            //    {
            //        Environment.Exit(0);
            //    }

            //    StepOneWindow stepOneWindow = new StepOneWindow();
            //    bool? flag1 = stepOneWindow.ShowDialog();
            //    if (flag1.HasValue && flag1 == false)
            //    {
            //        Environment.Exit(0);
            //    }

            //    StepTwoWindow stepTwoWindow = new StepTwoWindow();
            //    bool? flag2 = stepTwoWindow.ShowDialog();
            //    if (flag2.HasValue && flag2 == false)
            //    {
            //        Environment.Exit(0);
            //    }
            //}

            // 方式二
            {
                Type[] types = Assembly.GetExecutingAssembly().GetTypes();

                string content = File.ReadAllText("Config/AppSettings.json");
                JObject jObject = JObject.Parse(content);
                JToken? jToken = jObject.GetValue("StartWindows");
                if (jToken.Type == JTokenType.Array)
                {
                    foreach (var item in jToken)
                    {
                        string? name = item.Value<string>("Name");
                        bool? isShow = item.Value<bool>("IsShow");

                        bool cond1 = string.IsNullOrEmpty(name);
                        bool cond2 = !isShow.HasValue || isShow == false;
                        if (cond1 || cond2)
                            continue;

                        Type type = types.First(p => p.FullName == name);
                        if (type != null)
                        {
                            Window? instance = (Window)Activator.CreateInstance(type);
                            bool? flag = instance.ShowDialog();
                            if (flag.HasValue && flag == false)
                            {
                                Environment.Exit(0);
                            }
                        }
                    }
                }
            }


            Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;


            // 使用互斥体保证只启动一个应用实例
            // 方式一
            //Mutex mutex = new Mutex(true, "zidingyikongjian", out bool flag);
            //if (!flag)
            //{
            //    MessageBox.Show("current application instance has start up!");
            //    Environment.Exit(0);
            //}


            // 方式二
            //{
            //    Process process = Process.GetCurrentProcess();

            //    Process[] processes = Process.GetProcessesByName(process.ProcessName);
            //    bool flag = processes.Any(p => p.Id != process.Id);
            //    if (flag)
            //    {
            //        // 将之前打开的窗口设置为激活状态
            //        Environment.Exit(0);
            //    }
            //}

            base.OnStartup(e);
        }
    }

}
